package com.sz.app.edi.rest.api;

import com.sz.app.edi.job.rpt.SaleRptJobSchedulerImpl;
import com.sz.biz.rpt.entity.RptCalcJob;
import com.sz.biz.rpt.service.RptCalcJobService;
import com.sz.common.base.dto.ResultDto;
import com.sz.common.base.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.Date;

/**
 * Function: 驾驶舱统计. <br>
 * Date: 2017年12月15日 15:00 <br>
 * Author: wentao.chang
 */
@RestController
@RequestMapping("${api}/rpt")
@Api(description = " ", tags = "003、驾驶舱统计")
public class RptCalcController {

    @Qualifier("saleRptJobScheduler")
    @Autowired
    private SaleRptJobSchedulerImpl saleRptJobScheduler;

    @Autowired
    private RptCalcJobService rptCalcJobService;

    @ApiOperation(value = "报表统计", notes = "按时间统计业务类型、交易额、订单量、货量、供应商数量、客户数量等")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "date", value = "date yyyy-MM-dd", required = true, dataType = "string", paramType = "query") })
    @RequestMapping(value = "/calc", method = {RequestMethod.GET})
    public ResultDto calc(@RequestParam String date) throws ParseException {
        Date dat;
        try {
            dat = DateUtils.parseDate(date, DateUtils.FORMAT_SIMPLE);
        } catch (Exception e) {
            ResultDto result = new ResultDto();
            result.setData("参数错误，date:yyyy-MM-dd");
            return result;
        }
        rptCalcJobService.deleteByDate(dat);

        saleRptJobScheduler.calcDateRpt(dat);
        ResultDto result = new ResultDto();
        result.setData("执行成功");
        return result;
    }

}
